package cn.cjlmonster.demo02.ui;

import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;

import java.util.Locale;

import cn.cjlmonster.demo02.R;
import cn.cjlmonster.demo02.base.BaseAppCompatActivity;
import cn.cjlmonster.demo02.dialog.CalendarDialog;

/**
 * cn.cjlmonster.demo02.ui
 *
 * @author cjl
 * @project Demo02
 * @date 2022/8/18 16:41
 */
public class CalendarTestActivity extends BaseAppCompatActivity {

    private static final String TAG = "日历测试";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_calendar_test;
    }

    @Override
    protected boolean canBack() {
        return true;
    }

    private ImageButton ib_pre;
    private TextView tv_current_date;
    private ImageButton ib_next;
    private CalendarView calendarView;
    private Button btn_select_date;

    private Calendar calendar;

    @Override
    protected void initView() {
        ib_pre = findViewById(R.id.ib_pre);
        tv_current_date = findViewById(R.id.tv_current_date);
        ib_next = findViewById(R.id.ib_next);
        calendarView = findViewById(R.id.calendarView);
        btn_select_date = findViewById(R.id.btn_select_date);
        calendar = calendarView.getSelectedCalendar();
//        calendar.setDay(10);
        setCurrentDate(calendar.getYear(), calendar.getMonth());
        addMasks();
    }

    @Override
    protected void initEvents() {
        ib_pre.setOnClickListener(v -> calendarView.scrollToPre(true));
        ib_next.setOnClickListener(v -> calendarView.scrollToNext(true));
        calendarView.setOnMonthChangeListener(this::setCurrentDate);
        calendarView.setOnCalendarSelectListener(new CalendarView.OnCalendarSelectListener() {
            @Override
            public void onCalendarOutOfRange(Calendar calendar) {

            }

            @Override
            public void onCalendarSelect(Calendar calendar, boolean isClick) {
                if (isClick)
                    showToast(String.format(Locale.CHINA, "%04d-%02d-%02d", calendar.getYear(), calendar.getMonth(), calendar.getDay()));
            }
        });
        btn_select_date.setOnClickListener(v -> CalendarDialog.showDialog(this));
    }

    private void addMasks() {
        int year = calendar.getYear();
        int month = calendar.getMonth();
        int day = calendar.getDay();

        Calendar c1 = new Calendar();
        c1.setYear(year);
        c1.setMonth(month);
        c1.setDay(day - 3);
        calendarView.addSchemeDate(c1);

        Calendar c2 = new Calendar();
        c2.setYear(year);
        c2.setMonth(month);
        c2.setDay(day - 4);
        calendarView.addSchemeDate(c2);

        Calendar c3 = new Calendar();
        c3.setYear(year);
        c3.setMonth(month);
        c3.setDay(day - 5);
        calendarView.addSchemeDate(c3);

        Calendar c4 = new Calendar();
        c4.setYear(year);
        c4.setMonth(month);
        c4.setDay(day - 7);
        calendarView.addSchemeDate(c4);
    }

    private void setCurrentDate(int year, int month) {
        String currentData = String.format(Locale.CHINA, "%04d/%02d", year, month);
        tv_current_date.setText(currentData);
    }
}
